let app = getApp()
const cookieUtil = require("../../utils/cookie.js")

Page({
  data: {
	user_like_club_list: []  
  },
  //用户取消收藏
  OnOrder (e) {
	//获得活动对应下标
	const clubId = e.currentTarget.dataset.id//这个活动的id
	const idx = e.currentTarget.dataset.idx//列表的索引
	//封装数据
	const url = app.globalData.baseUrl + app.globalData.clublikestatusUrl
	const cookie = cookieUtil.getCookieFromStorage().data
	let headers = {}
	let data = {}
	headers.cookie = cookie
	data.clubId = clubId
	console.log(data)
	console.log(headers)
	my.request({
		data,
		url,
		headers,
		method: "GET",
		success: (res) => {
			console.log(res)
			my.showToast({
				type: 'success',
				content: res.data.message,
				duration: 2000,
			})
			//图标状态
			let user_like_club_list = this.data.user_like_club_list
			console.log(user_like_club_list)
			console.log(idx)
			if (res.data.message.search("取消") != -1) {
				//user_like_club_list[idx]["likeStatus"] = false
				//删除该条记录
				user_like_club_list.splice(idx,1)
				this.setData({
					user_like_club_list
				})
			} else {
				user_like_club_list[idx]["likeStatus"] = true
			}
			console.log(user_like_club_list)
			console.log("============================")
			this.setData({
				user_like_club_list
			})
		}
	})

	// if (getCurrentPages().length != 0) {
	// 	//刷新当前页面的数据
	// 	getCurrentPages()[getCurrentPages().length - 1].onLoad()
	// }
  },
  onLoad() {
	  // my.hideTabBar({
		 //  animation: true,
	  // });
	  const url = app.globalData.baseUrl + app.globalData.likeClubUrl
	  //从缓存中拿取cookie
	  const cookie = cookieUtil.getCookieFromStorage().data
	  //如果不存在cookie则让其注册/登录
	  if( !cookie ){
		  
		//展示什么都没有的图片
		//
		//
		//
		
	  	// my.navigateTo({
	  	// 	url:"/pages/register/register"
	  	// })	
	  }
	  
	  //从服务起获得数据
	  let promise = new Promise((resolve, reject) => {
	  	
	  	let headers = {}
	  	headers.cookie = cookie

	  	my.request({
	  		url,
	  		headers,
	  		method: "GET",
	  		success: (res) => {
	  			resolve(res)
	  		}
	  	})
	  })

	  promise.then((res) => {
		  console.log(res)
	  	//如果未登录则进行跳转
	  	if (res.data.status == "fail") {
			cookieUtil.setCookieToStorage("")
			app.globalData.userStatus = false
	  		my.navigateTo({
	  			url:"/pages/register/register"
	  		})		
	  	}
		
		let user_like_club_list = []
		if (res.data.length) {
			user_like_club_list = Object.values(res.data)
			console.log(user_like_club_list)
			user_like_club_list.map((obj) => obj["likeStatus"] = true)
		}
		console.log(user_like_club_list)
		this.setData({
			user_like_club_list
		})
	  })
  },
})
